// 防抖函数
function debounce(func, delay) {
  let timer = null;
  return function () {
    clearTimeout(timer);
    timer = setTimeout(() => {
      func.apply(this, arguments);
    }, delay);
  };
};
// 节流函数
function throttle(func, delay) {
  let timer = null;
  return function () {
    if (!timer) {
      timer = setTimeout(() => {
        func.apply(this, arguments);
        timer = null;
      }, delay);
    }
  };
};
// 使用节流函数处理数据收集
function myChange() {
  return debounce(function (e) {
    let field = 'form.' + e.currentTarget.dataset.field;
    let value = e.detail;
    this.setData({
      [field]: value
    });
  }, 500) // 设置节流的时间间隔为500毫秒
};
let data_time = {
  show_data: false,
  currentDate: new Date().getTime(),
  minDate: new Date().getTime(),
}
let formatter = (type, value) => {
  if (type === 'year') {
    return `${value}年`;
  }
  if (type === 'month') {
    return `${value}月`;
  }
  if (type === 'day') {
    return `${value}日`;
  }
  if (type === 'hour') {
    return `${value}小时`;
  }
  if (type === 'minute') {
    return `${value}分`;
  }
};
// 请假类型
let holiday_type = ["课假", "病假", "事假"];
// 是否离校
let leaving_school = ["是", "否"];

// 分割大的base64数据为块
function splitBase64IntoChunks(base64Data, chunkSize) {
  const totalChunks = Math.ceil(base64Data.length / chunkSize);
  const chunks = [];
  for (let i = 0; i < totalChunks; i++) {
    const start = i * chunkSize;
    const end = start + chunkSize;
    const chunk = base64Data.slice(start, end);
    chunks.push(chunk);
  }
  return chunks;
};
// 身份
let identity = ["学生","老师","管理员"]
// 菜单模块
let level = ["否","是"]
// 打乱数组的函数
function shuffleArray(array) {
  for (let i = array.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [array[i], array[j]] = [array[j], array[i]];
  }
  return array;
};
export {
  throttle,
  debounce,
  myChange,
  data_time,
  formatter,
  holiday_type,
  leaving_school,
  splitBase64IntoChunks,
  identity,
  level,
  shuffleArray,
}